#include<bits/stdc++.h>
#define ll long long
#define str string
#define db double
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define ms(x,t) memset(x,t,sizeof(x))
using namespace std;
const int maxn = 110+10;
const int maxm = 2e5+10;
const ll INF = 0x3f3f3f3f;
const ll mod = 1e9+7;
int n,k;
int a[maxn];
map<int,int> mp;
set<int> st[maxn]; 
ll ans = INF * INF;
#define IT map<int,int>::iterator
void dfs2(int x) {
	if(x == n+1) {
		ll sum = 0;
		for(int i = 1; i <= k; i++) {
			sum += *st[i].rbegin()-*st[i].begin();   
		}
		ans = min(ans,sum);
		return ;
	}
	for(int i = 1; i <= k; i++) if(st[i].size() < n/k) if(!st[i].count(a[x])){
		st[i].insert(a[x]);
		dfs2(x+1);
		st[i].erase(a[x]);
	}
}
void Solve() {
	cin >> n >> k;
	for(int i = 1; i <= n; i++) {
		cin >> a[i]; mp[a[i]]++;
		if(mp[a[i]] > k) {
			cout<<-1<<'\n'; return ;
		}
	}
	sort(a+1,a+n+1);
	dfs2(1);
	cout<<ans<<'\n';
}
int main(){
	freopen("diyiti.in","r",stdin); freopen("diyiti.out","w",stdout);
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//	int tt; cin >> tt; while(tt--)
	Solve();
	return 0;
}
/*


*/
